package com.study.it.ui.controller;

import com.study.it.dubbo.face.AccountDubbo;
import com.study.it.dubbo.face.RecordDubbo;
import com.study.it.entity.Account;
import com.study.it.hystrix.command.DubboCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    @Qualifier("accountDubboService")
    private AccountDubbo accountDubboService=null;
    @Autowired
    @Qualifier("recordDubboService")
    private RecordDubbo recordDubboService=null;


    @RequestMapping(value="/add",method = RequestMethod.GET)
    public String addAcount()
    {
        return "account/add";
    }
    @RequestMapping(value="/add",method = RequestMethod.POST)
    public String addAcount(String cardno,Float money)
    {
        //表面上看这是普通的代码，但是我告诉你的是，这个方法在另外一台机器上
        //Account acc=accountDubboService.add(cardno,money);
        DubboCommand<Account> cmd=new DubboCommand<Account>(this.accountDubboService,"add",new Object[]{cardno,money},null);
        cmd.execute();
        return "redirect:list";
    }
    @RequestMapping(value="/list",method = RequestMethod.GET)
    public String showList(String cardno,Model m)
    {
        DubboCommand<List<Account>> cmd=new DubboCommand<List<Account>>(
                this.accountDubboService
                ,"findList"
                ,null
                ,new ArrayList<Account>());
        m.addAttribute("list",cmd.execute());
        return "account/list";
    }
}
